//
//  ViewController.m
//  Archive
//
//  Created by MyMac on 15/8/21.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//


#define KUrl @"http://api.miaotu.com/v1.1/yueyou/list?latitude=39.998387&longitude=116.345638&num=10&page=1&token=a068475e-3ff7-11e5-9176-00163e002e59"

#define KNextUrl @"http://www.baidu.com"


#import "ViewController.h"
#import "Peasou.h"
#import "EasouManager.h"
@interface ViewController ()

@property(nonatomic,strong)Peasou *peasou;

@property(nonatomic,strong)NSData *data1;

@property(nonatomic,strong)NSData *data2;

@end

@implementation ViewController


- (void)dealloc
{
    
    _peasou = nil;
    NSLog(@"%@--------dealloc",NSStringFromClass([self class]));
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btn.frame = CGRectMake(100, 200, 100, 100);
    
    btn.backgroundColor = [UIColor redColor];
    
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn];
    
    __weak typeof(self) weakSelf = self;

    
   Peasou *peason = [self unarchive];
    
    if(!peason){
        
        
        [[EasouManager manager] Get:KUrl andWithSucceedBlock:^(NSData *data) {
            
            
            [weakSelf archive:data];
            
            
        } failedBlock:^(NSError *error) {
            
        }];
        
        

        
        
    }
    
    
    
}

- (void)btnClick{
    
    
    __weak typeof(self) weakSelf = self;
    
   
    
    [[EasouManager manager] Get:KUrl andWithSucceedBlock:^(NSData *data) {
        
        weakSelf.data2 = data;
        
        Peasou *peasou = [weakSelf unarchive];
        
        weakSelf.data1 = peasou.data;
        
        
        if(self.data1&&self.data2){
            
            NSString *str1 = [[NSString alloc] initWithData:self.data1 encoding:NSUTF8StringEncoding];
            
            NSString *str2 = [[NSString alloc] initWithData:self.data2 encoding:NSUTF8StringEncoding];
            
            if([str1 isEqualToString:str2]){
                
                NSLog(@"equal");
                
                
            }else{
                
                
                
                NSLog(@"diffence");
                
                
            }
            
            
            
            
        }
        
        

        
        
        
        
        
    } failedBlock:^(NSError *error) {
        
        
        NSLog(@"%@",[error localizedDescription]);
        
        
        
    }];
    
    
    
    
    
}


- (void)archive:(NSData *)data
{
    
    Peasou *peason = [[Peasou alloc] init];
    
    peason.data = data;
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"User.data"];
    
    
    [NSKeyedArchiver archiveRootObject:peason toFile:path];
    
    
    
}


- (Peasou *)unarchive
{
    
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"User.data"];
    
    
    Peasou *peasou = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    return peasou;
    
    
    
}

@end
